package controller;

import java.sql.*;
import java.util.Dictionary;
import java.util.HashMap;

public class DBController {
    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306";

    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "qwer1234";

    private static DBController ourInstance = new DBController();

    public static DBController getInstance() {
        return ourInstance;
    }

    private DBController() {

    }

    // 注册用户信息


    // 登录并返回信息
    public HashMap<String,String> getLoginUserInfo(String uniquenes,String password) {
        Connection conn = null;
        Statement stmt = null;
        HashMap<String,String> mapUserInfo=new HashMap<String,String>();

        try{
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "select `uid`,`email`,`nickname`,`avator_icon`,`city`,`address` from user_scheme.user_info where `uid` = " + uniquenes;
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索d
                String u_id  = rs.getString("uid");
                String name = rs.getString("nickname");
                String url = rs.getString("avator_icon");
                String city = rs.getString("city");
                String address = rs.getString("address");
                String email = rs.getString("email");

                mapUserInfo.put("uid",u_id);
                mapUserInfo.put("name",name);
                mapUserInfo.put("url",url);
                mapUserInfo.put("city",city);
                mapUserInfo.put("address",address);
                mapUserInfo.put("email",email);

                System.out.println("");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
            System.out.println("Goodbye!");
            return  mapUserInfo;
        }


    }

    // 根据 uid 查找用户信息
    public HashMap<String,String> getuserinfo(String uid) {
        Connection conn = null;
        Statement stmt = null;
        HashMap<String,String> mapUserInfo=new HashMap<String,String>();

        try{
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "select `uid`,`email`,`nickname`,`avator_icon`,`city`,`address` from user_scheme.user_info where `uid` = " + uid;
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索d
                String u_id  = rs.getString("uid");
                String name = rs.getString("nickname");
                String url = rs.getString("avator_icon");
                String city = rs.getString("city");
                String address = rs.getString("address");
                String email = rs.getString("email");

                mapUserInfo.put("uid",u_id);
                mapUserInfo.put("name",name);
                mapUserInfo.put("url",url);
                mapUserInfo.put("city",city);
                mapUserInfo.put("address",address);
                mapUserInfo.put("email",email);

                System.out.println("");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
            System.out.println("Goodbye!");
            return  mapUserInfo;
        }


    }
}
